package com.czm.art_light.controller;

import com.czm.art_light.constant.ApiRouterConsts;
import com.czm.art_light.constant.ArtErrorCodeEnum;
import com.czm.art_light.exception.BusinessException;
import com.czm.art_light.param.request.ArtSearchReqDto;
import com.czm.art_light.param.response.ArtSearchRespDto;
import com.czm.art_light.param.response.RestResp;
import com.czm.art_light.service.ArtSearchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 搜索模块 API 控制器
 */
@Tag(name = "搜索管理", description = "搜索相关接口")
@RestController
@RequestMapping(ApiRouterConsts.SEARCH_API_URL_PREFIX)
public class ArtSearchController {
    @Autowired
    private ArtSearchService artSearchService;

    /**
     * 根据关键字进行搜索
     */
    @Operation(summary = "根据关键字进行搜索")
    @PostMapping("/search")
    public RestResp<ArtSearchRespDto> searchByKeyword(@Valid  @RequestBody ArtSearchReqDto artSearchReqDto) {
        // 去除前后空格
        if (artSearchReqDto.getKeyword() != null) {
            artSearchReqDto.setKeyword(artSearchReqDto.getKeyword().strip());
        } else {
            throw new BusinessException(ArtErrorCodeEnum.INVALID_KEYWORD);
        }

        // 空格作为搜索条件返回空结果
        if (artSearchReqDto.getKeyword().isEmpty()) {
            throw new BusinessException(ArtErrorCodeEnum.INVALID_KEYWORD);
        }
        ArtSearchRespDto artSearchRespDtos = artSearchService.searchByKeyword(artSearchReqDto);
        return RestResp.ok(artSearchRespDtos);
    }

}
